import bean.Developer;
import bean.Person;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zhangxupeng
 * @date:2019/5/10
 * @Email: zhangxupeng@bilibili.com
 **/
public class TestStreamsmap {
    public static void main(String[] args) {
        listItemMapper();

        changedByStreamMapper();

    }

    private static void changedByStreamMapper() {
        List<Person> persons = Arrays.asList(
                new Person("mkyong", 30),
                new Person("jack", 27),
                new Person("lawrence", 33)
        );

        // convert inside the map() method directly.
        List<Developer> result = persons.stream().map(temp -> {
            Developer obj = new Developer();
            obj.setName(temp.getName());
            obj.setAge(temp.getAge());
            obj.setSalary(new BigDecimal(2000));
            return obj;
        }).collect(Collectors.toList());

        System.out.println(result);
    }


    private static void listItemMapper() {
        List<String> alpha = Arrays.asList("a", "b", "c", "d");

        //Java8 之前的操作
        List<String> alphaUpper = new ArrayList<>();
        for (String s : alpha) {
            alphaUpper.add(s.toUpperCase());
        }

        System.out.println(alpha); //[a, b, c, d]
        System.out.println(alphaUpper); //[A, B, C, D]

        // Java 8 里面的操作
        List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(collect); //[A, B, C, D]

        // 另外, streams 允许你做任意的转化.
        List<Integer> num = Arrays.asList(1,2,3,4,5);
        List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList());
        System.out.println(collect1); //[2, 4, 6, 8, 10]
    }
}
